# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout

#Facebook
APP_ID = '211565568907437'
APP_SECRET = 'e58724edc7f3a3c6730b564b07a7ee99'

# This is a tuple of the page tabs, the first entry is the backend name, the second is the friendly name
TABS = [
    ('profile','Your Beer'),
    ('about','About'),
    ('reviews','Beer Reviews'),

]

#This is called during any page request and authenticates users if they are logging in
# or registering. It will also add any data that is common among the pages
def common(request):
    common = {}
    common['is_authenticated'] = request.user.is_authenticated()
    common['username'] = None
    common['email'] = None
    common['tabs'] = TABS
    if request.method == 'GET':
        if request.GET.get('type') == 'logout':
            logout(request)
            common['is_authenticated'] = request.user.is_authenticated()
    if common['is_authenticated']:
        common['username'] = request.user.username
        common['email'] = request.user.email
            
    if request.method == 'POST':
        if request.POST.get('type') == 'login':
            email = request.POST.get('email')
            password = request.POST.get('password')
            u = User.objects.get(email__exact=email)
            username = None
            user = None
            if u:
                username = u.username
                user = authenticate(username=username, password=password)
                if user.is_active:
                    login(request, user)
                    common['is_authenticated'] = True
                    common['username'] = username
                    common['email'] = email
                else:
                    common['is_authenticated'] = False
            else:
                common['is_authenticated'] = False
        elif request.POST.get('type') == 'register':
            email = request.POST.get('email')
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = User.objects.create_user(username, email, password)
            user.save()
            login(request, user)
            common['is_authenticated'] = True
            common['email'] = email
            common['username'] = username
            
    return common
        




# This handles the personal profile page
def profile(request, tab_name):
    c = common(request)
    c['currtab'] = tab_name
    return render_to_response('tabs/profile.html', c ,context_instance=RequestContext(request))
# This handles the review page with all the different beers
def reviews(request, tab_name):
    c = common(request)
    c['currtab'] = tab_name
    return render_to_response('tabs/reviews.html', c ,context_instance=RequestContext(request))
# This is the about page
def about(request, tab_name):
    c = common(request)
    c['currtab'] = tab_name
    return render_to_response('tabs/about.html', c, context_instance=RequestContext(request))
